package com.heima.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.config.JwtProperties;
import com.heima.user.domain.dto.UserLoginDto;
import com.heima.user.domain.po.User;
import com.heima.user.mapper.UserMapper;
import com.heima.user.service.IUserService;
import com.heima.user.utils.JwtTool;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final JwtTool jwtTool;

    private final JwtProperties properties;

    @Override
    public String login(UserLoginDto dto) {
        String mobile = dto.getMobile();
        String password = dto.getPwd();
        if (mobile == null || password == null){
            throw new RuntimeException("手机号和密码不能为空");
        }
        User user = lambdaQuery().eq(User::getMobile,mobile).eq(User::getPwd,password).one();
        if (user == null){
            throw new RuntimeException("手机号或密码错误");
        }
        String token = jwtTool.createToken(user.getId(), properties.getTokenTTL());

        return token;
    }
}
